package MODULES.MOD_XML.VIEWS;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import MODULES.MOD_XML.CONTROLLERS.XMLControllers;
import MODULES.MOD_XML.MODELS.Student;

public class XMLViews01 extends JFrame {

    private final JPanel contentPane;
    private final JTextField txtRollNo;
    private final JTextField txtFullName;
    private final JTextField txtAge;
    private final JTextField txtMark;
    private final JTextField txtInfo;
    private final List<Student> lisStudent;
    private final JButton btnGhiDLiu;
    private int iCounter = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    XMLViews01 frame = new XMLViews01();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public XMLViews01() {
        lisStudent = new ArrayList<Student>();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 538, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new TitledBorder(null, "TH\u00CAM SINH VI\u00CAN", TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnThem = new JButton("Thêm thông tin Sinh Viên");
        btnThem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 1. Sử dụng biểu thức chính qui Regular Expression
                // 2. Khai báo & khởi tạo đối tượng Sinh Viên
                iCounter++;
                Student objStudent = new Student();
                objStudent.setId(iCounter);
                objStudent.setRollNo("" + txtRollNo.getText());
                objStudent.setFullName("" + txtFullName.getText());
                objStudent.setAge(Integer.parseInt("" + txtAge.getText()));
                objStudent.setMark(Double.parseDouble("" + txtMark.getText()));

                // 3. Đưa vào danh sách sinh viên
                lisStudent.add(objStudent);

                // 4. Hiển thị số sinh viên trong danh sách
                txtInfo.setText("Có " + lisStudent.size() + " sinh viên trong danh sách!");

                // 5.
                txtRollNo.setText("");
                txtFullName.setText("");
                txtAge.setText("");
                txtMark.setText("");
            }
        });
        btnThem.setForeground(new Color(255, 255, 255));
        btnThem.setBackground(new Color(0, 139, 139));
        btnThem.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnThem.setBounds(297, 95, 215, 33);
        contentPane.add(btnThem);

        JLabel lblNewLabel = new JLabel("RollNo :");
        lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel.setBounds(18, 23, 57, 23);
        contentPane.add(lblNewLabel);

        txtRollNo = new JTextField();
        txtRollNo.setBackground(Color.ORANGE);
        txtRollNo.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtRollNo.setBounds(70, 25, 74, 20);
        contentPane.add(txtRollNo);
        txtRollNo.setColumns(10);

        JLabel lblFullname = new JLabel("FullName :");
        lblFullname.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblFullname.setBounds(155, 23, 74, 23);
        contentPane.add(lblFullname);

        txtFullName = new JTextField();
        txtFullName.setBackground(SystemColor.textHighlight);
        txtFullName.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtFullName.setColumns(10);
        txtFullName.setBounds(229, 25, 283, 20);
        contentPane.add(txtFullName);

        JLabel lblAge = new JLabel("Age :");
        lblAge.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblAge.setBounds(18, 62, 57, 23);
        contentPane.add(lblAge);

        txtAge = new JTextField();
        txtAge.setBackground(Color.ORANGE);
        txtAge.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtAge.setColumns(10);
        txtAge.setBounds(70, 64, 74, 20);
        contentPane.add(txtAge);

        JLabel lblMark = new JLabel("Mark :");
        lblMark.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblMark.setBounds(155, 62, 57, 23);
        contentPane.add(lblMark);

        txtMark = new JTextField();
        txtMark.setBackground(SystemColor.textHighlight);
        txtMark.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtMark.setColumns(10);
        txtMark.setBounds(229, 64, 283, 20);
        contentPane.add(txtMark);

        txtInfo = new JTextField();
        txtInfo.setBackground(new Color(0, 0, 0));
        txtInfo.setForeground(new Color(255, 255, 255));
        txtInfo.setEditable(false);
        txtInfo.setEnabled(false);
        txtInfo.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtInfo.setColumns(10);
        txtInfo.setBounds(18, 139, 494, 112);
        contentPane.add(txtInfo);

        btnGhiDLiu = new JButton("Ghi dữ liệu ra file XML");
        btnGhiDLiu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (lisStudent.size() > 0) {
                    // 1. Sử dụng Components để hiển cửa sổ lưu files
                    JFileChooser objFile = new JFileChooser();
                    objFile.setCurrentDirectory(new java.io.File("."));
                    objFile.setDialogTitle("Chọn file: ");
                    objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    String[] EXTENSION = new String[] { "xml" };
                    FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                    objFile.setFileFilter(filter);
                    objFile.setMultiSelectionEnabled(false);

                    int returnVal = objFile.showSaveDialog(getParent());
                    String ParentPath = "";
                    String FileName = "";
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        ParentPath = objFile.getSelectedFile().getParent();
                        FileName = objFile.getSelectedFile().getName();

                        // 2. Khai báo & khởi tạo Controllers
                        XMLControllers objController = new XMLControllers();

                        // 3. Ủy quyền thực thi nghiệp vụ
                        try {
                            List<Object> lisField = new ArrayList<Object>();
                            lisField.add("Id");
                            lisField.add("RollNo");
                            lisField.add("FullName");
                            lisField.add("Age");
                            lisField.add("Mark");
                            List<List<Object>> lisValue = objController.ExecOfGetListObject(lisStudent);
                            String pathFile = ParentPath + "/" + FileName;
                            boolean bResults = objController.ExecOfWriteXML(lisField, lisValue, "iMIC", "Student",
                                    pathFile, true);
                            if (bResults)
                                JOptionPane.showMessageDialog(getParent(), "Ghi dữ liệu ra files thành công!");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(getParent(),
                            "Yêu cầu không thể thực hiện do chưa có bất kỳ sinh viên nào trong danh sách!");
                }
            }
        });
        btnGhiDLiu.setForeground(Color.WHITE);
        btnGhiDLiu.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnGhiDLiu.setBackground(new Color(255, 0, 255));
        btnGhiDLiu.setBounds(80, 95, 215, 33);
        contentPane.add(btnGhiDLiu);
    }
}
